/**
 * 
 */
package plar.core;
import java.awt.Point;
import java.awt.Polygon;
import java.util.HashMap;

import plar.SpriteSet;

/**
 * @author spocchio
 *
 */
public class Element {
	
	public HashMap <String, Callback> actions;
	public HashMap  <String, Object> properties;
	public SpriteSet currentSpriteSet;
	
	
	public Element()
	{
	   actions = new HashMap <String, Callback>();
	   properties = new  HashMap <String, Object>();
	   currentSpriteSet = new SpriteSet();
	   setPosition(new Point(0,0));
	   setSpeed(new Point(0,0));
	   
	   
	   
	}
	
	public void setSpeed(Point p)
	{
		setProperty("speed",p);
		
	}

	public Point getSpeed()
	{
		return (Point)getProperty("speed");
	}
	
	public void setPosition(Point p)
	{
		setProperty("position",p);
		
	}

	public Point getPosition()
	{
		return (Point)getProperty("position");
	}
	
	public void setProperty(String s,Object o)
	{
		properties.put(s, o);
	}
	
	public Object getProperty(String s)
	{
		return properties.get(s);
	}
	public Callback addAction(String name, Callback a)
	{
		actions.put(name,a);
		return a;
	}
	
	
	public Callback addAction(String name)
	{
		class Azione implements Callback
		{
			public Azione()
			{
			
			}
			 public HashMap call(HashMap parameters)
			 {
				 Common.info("anonymous info called");
				 return null;
			 }
		}
		
		Azione a = new Azione();
		actions.put(name,a);
		return a;
	}
	
	public Sprite getCurrentSprite()
	{
	   return currentSpriteSet.getCurrentSprite();	
	}
	public boolean isColliding(Element e)
	{
   	   
       Polygon i = getCurrentSprite().getShape();	
	   Polygon enemy = e.getCurrentSprite().getShape();
	   enemy.translate(e.getPosition().x, e.getPosition().y);
	   i.translate(getPosition().x, getPosition().y);
	   
	   if(Common.polygonIntersects(i,enemy) ) return true;
	   else return false;
		   
	}

	public HashMap interaction(Element e,HashMap event)
	{
	   for(String k:e.properties.keySet())
	   {
		   if(actions.containsKey(k)) runAction(getAction(k),event);		   
	   }
	   return null;
	   
	}
	public Callback getAction(String k)
	{
		return actions.get(k);
	}
	public void runAction(Callback c,HashMap p)
	{
		c.call(p);
	}

	public void changeSpriteSet(SpriteSet sp)
	{
		
	}

    
	
}
